package com.hwadeefsc.springboot.controller;

import com.alibaba.fastjson.JSONArray;
import com.hwadeefsc.springboot.entity.RewardsPunishments;
import com.hwadeefsc.springboot.service.RewardsPunishmentsService;
import com.hwadeefsc.springboot.utils.result.DataResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (RewardsPunishments)表控制层
 *
 * @author makejava
 * @since 2024-07-09 21:43:27
 */
@RestController
@RequestMapping("RewardsPunishments")
public class RewardsPunishmentsController {
    /**
     * 服务对象
     */
    @Resource
    private RewardsPunishmentsService rewardsPunishmentsService;

    /**
     * 分页查询
     *
     * @param rewardsPunishments 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<RewardsPunishments>> queryByPage(RewardsPunishments rewardsPunishments, PageRequest pageRequest) {
        return ResponseEntity.ok(this.rewardsPunishmentsService.queryByPage(rewardsPunishments, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<RewardsPunishments> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.rewardsPunishmentsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param rewardsPunishments 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<RewardsPunishments> add(RewardsPunishments rewardsPunishments) {
        return ResponseEntity.ok(this.rewardsPunishmentsService.insert(rewardsPunishments));
    }

    /**
     * 编辑数据
     *
     * @param rewardsPunishments 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<RewardsPunishments> edit(RewardsPunishments rewardsPunishments) {
        return ResponseEntity.ok(this.rewardsPunishmentsService.update(rewardsPunishments));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.rewardsPunishmentsService.deleteById(id));
    }

      //根据type查询
    @RequestMapping("type={type}")
    public DataResult<JSONArray> Reward(@PathVariable("type") String type){
        //定义type为1时为惩罚type为0时为奖励
        if(Objects.equals(type, "0")){
                   type="奖励";
               }else if(Objects.equals(type, "1")){
                     type="惩罚";
               }
        return DataResult.successByDataArray(rewardsPunishmentsService.queryBytype(type)) ;
    }

    //根据学生id查询
    @RequestMapping("id={stdId}")
    public DataResult<JSONArray> queryByStdId(@PathVariable("stdId") int stdId){
        return DataResult.successByDataArray(rewardsPunishmentsService.queryByStdId(stdId)) ;
    }
}

